// Gregory's Object Oriented Programming Tracker (aka GOOPTracker), Copyright (c) 2008 Gregory Athons, <http://gregathons.com> MIT Style License

(function() {

	var version = "0.1";
	
	// first, we check if the dom is ready - we have a counter so it stops eventually
	var domready_counter = 0;
	var domready = setInterval(function() {
		
		// check if we found the end body tag
		if(document.getElementsByTagName("HTML")[0].innerHTML.toLowerCase().indexOf("</body>") != -1) {

			// initiate the ad tracker and stop the setInterval
			setTimeout(phase1, 500);
			clearInterval(domready);
			
		} else {
			
			// increment the counter
			domready_counter++;
			if(domready_counter > 20) {
				clearInterval(domready_counter);
			}
			
		}
		
	}, 500);
	
	// sets the GOOPTracker object and properties
	function phase1() {
		
		// was the GOOPTracker created? If not lets create one
		if(typeof GOOPTracker == "undefined") {
			GOOPTracker = new Object();
		}
		
		// check if GOOPTracker properties were set
		
		// base url
		if(typeof GOOPTracker.base_url == "undefined") {
			GOOPTracker.base_url = "http://" + document.domain + "/";
		}
		
		// data tracking page, this where the data is sent to
		if(typeof GOOPTracker.tracker == "undefined") {
			GOOPTracker.tracker = "gooptracker.php";
		}
		
		// url of page
		if(typeof GOOPTracker.url == "undefined") {
			GOOPTracker.url = window.location;
		}
		
		// title of page
		if(typeof GOOPTracker._title == "undefined") {
			GOOPTracker._title = document.title;
		}
		
		// custom variables, set as an array... whatever else you want to track
		if(typeof GOOPTracker.custom == "undefined") {
			GOOPTracker.custom = new Array();
		}
		
		// set up keys
		GOOPTracker.keys = new Object();
		GOOPTracker.keys[17] = false; // control
		GOOPTracker.keys[82] = false; // R key
		GOOPTracker.keys[116] = false; // F5 key
		
		// now that is set up, lets start the second phase
		phase2();
		
	}
	
	// get all iframes and attach events to them
	function phase2() {
		
		var iframes = document.getElementsByTagName("IFRAME");
		var iframes_counter = iframes.length;

		for(var i = 0; i < iframes_counter; i++) {
			
			// holder
			var el = iframes[i];
			
			// check if its one of these
			if(el.src.toLowerCase().match(/google|overture|adbrite/)) {
				
				// wrap a div around it
				var div = document.createElement("DIV");
				div.style.margin = "0px";
				div.style.padding = "0px";

				// replace iframe with div
				el.parentNode.replaceChild(div,el);
				
				// now put iframe in div
				div.appendChild(el);
				
				// add the event
				if(div.addEventListener) {
					div.addEventListener("mouseover", rollOver, false);
					div.addEventListener("mouseout", rollOut, false);
				} else if(div.attachEvent) {
					div.attachEvent("onmouseover", rollOver);
					div.attachEvent("onmouseout", rollOut);
				} else {
					div["onmouseover"] = rollOver;
					div["onmouseout"] = rollOut;
				}
				
			} // end if
			
		}
		
		// now the third phase
		phase3();
		
	}
	
	// add the onunload, but check if one exists... if it does, grab the original function and add it to our own 
	function phase3() {

		// function holder
		var ftn = function() { };
		
		if(typeof window.onbeforeunload == "function") {
			ftn = window.onbeforeunload;
		}
		
		// now we create our onbeforeunload function
		window.onbeforeunload = function() {
			checkClick();
			ftn();
		}
		
		// almost done
		phase4();
		
	}
	
	// key press... did the user refresh the page? 
	function phase4() {
		
		// add event listeners again, for key presses
		if(window.addEventListener) {
			window.addEventListener("keydown", checkKeyDown, false);
			window.addEventListener("keyup", checkKeyUp, false);
		} else if(document.attachEvent) {
			document.attachEvent("onkeydown", checkKeyDown);
			document.attachEvent("onkeyup", checkKeyUp);
		} else {
			window["onkeydown"] = checkKeyDown;
			window["onkeyup"] = checkKeyUp;
		}
		
		// last one
		phase5();
		
	}
	
	// set up the path
	function phase5() {
		
		// attach custom variables
		var custom = '';
		
		for(var i = 0; i < GOOPTracker.custom.length; i++) {
			custom += "&variable" + i + "=" + GOOPTracker.custom[i];
		}
		
		// this is the final path :)
		GOOPTracker.path = GOOPTracker.base_url + GOOPTracker.tracker + "?c=" + new Date().getTime() + "&url=" + GOOPTracker.url + "&title=" + GOOPTracker._title + custom;
		
	}
	
	
	// checks what keys were pressed
	function checkKeyDown(e) {
		
		var keyCode = (e.which ? e.which : event.keyCode);
		
		if(GOOPTracker.keys[keyCode] != "undefined") {
			GOOPTracker.keys[keyCode] = true;
		}
		
	}
	function checkKeyUp(e) {
		
		var keyCode = (e.which ? e.which : event.keyCode);

		if(GOOPTracker.keys[keyCode] != "undefined") {
			GOOPTracker.keys[keyCode] = false;
		}
		
	}
	
	// change it the boolean value
	function rollOver() {
		GOOPTracker.iframe = true;
	}
	function rollOut() {
		GOOPTracker.iframe = false;
	}
	
	
	// check if cursor is over iframe, and user isn't holding down any keys
	function checkClick() {
		
		// don't continue if not over iframe
		if(!GOOPTracker.iframe) { return; }
	
		// get the os so we can determine what keys to detect
		var os = navigator.userAgent.toLowerCase();
		if(os.indexOf("macintosh") != -1) {
			if(GOOPTracker.keys[82] || GOOPTracker.keys[116]) { return; }
		} else {
			if((GOOPTracker.keys[17] && GOOPTracker.keys[82]) || GOOPTracker.keys[116]) { return; }
		}
		
		// track it
		var img = new Image(1,1);
		img.src = GOOPTracker.path;
		img.onload = function() {
			return;
		}
			
	}

})();